package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.IProductDaoImp;
import com.qf.xiaomi.pojo.PageBen;
import com.qf.xiaomi.pojo.TbGoodsKz;
import com.qf.xiaomi.pojo.TbGoodsUnion;
import com.qf.xiaomi.utils.DruidDButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Created by 马英超 on 2022/8/17 19:31
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class ProductDaoImpl implements IProductDaoImp {
    /**
     * 分页查询
     * @param typeID
     * @param pageBen
     * @return
     */
    @Override
    public PageBen queryPageBen(String typeID, PageBen pageBen) {
        //参数处理
        final int id = Integer.parseInt(typeID);
        //查询产品数据sql
        String sql ="SELECT  a.id, b.name, b.pubdate, b.picture, b.price FROM  tb_goods a,  tb_goods_kz b,  tb_goods_type c WHERE a.kzid = b.id AND  b.typeid = c.id   AND STATUS = 1 AND c.id=? LIMIT ?,?";
        //查询总条数sql
        String sql1="SELECT count(0) as totalSize FROM  tb_goods a,  tb_goods_kz b,  tb_goods_type c WHERE a.kzid = b.id AND  b.typeid = c.id   AND STATUS = 1 AND c.id=?";

        //获取QueryRunner对象
        QueryRunner queryRunner=new QueryRunner();

        //执行查询
        try {

           //统计数据总条数
            final Object query = queryRunner.query(DruidDButils.getDruidConn(), sql1, new ScalarHandler(), id);
            final String s = String.valueOf(query);
            final int totalSize = Integer.parseInt(s);
            //偏移量计算：偏移量=(当前页-1)*pageSize
            final int offSetNum = (pageBen.getCurrentPage() - 1) * pageBen.getPageSize();
            //总页数的计算方式：3种
            //方式一：使用三元运算符
          //  final int i =(totalSize/ pageBen.getPageSize())+ (totalSize % pageBen.getPageSize()==0? 0 : 1);
            //方式二：使用Math向上取整
          //  final double ceil = Math.ceil((double)totalSize / pageBen.getPageSize());
            //方式三：天花板函数 假设总条数是n,每页显示x,需要多少页 (n+x-1)/x
            final int totalpage = (totalSize + pageBen.getPageSize() - 1) / pageBen.getPageSize();
            //查询商品数据
            final List<TbGoodsUnion> list = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanListHandler<>(TbGoodsUnion.class), id,offSetNum,pageBen.getPageSize());
            if (list!=null){
            //将商品数据放入PageBen对象内
                pageBen.setTotalSize(totalSize);
                pageBen.setList(list);
                pageBen.setTotalPage(totalpage);
                return pageBen;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据产品id查询产品信息
     * @param id
     * @return
     */
    @Override
    public TbGoodsKz queryProductById(int id) {
        String sql="SELECT * FROM tb_goods_kz t WHERE id=? ";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final TbGoodsKz tbGoodsKz = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanHandler<>(TbGoodsKz.class), id);
        if(tbGoodsKz!=null)
            return tbGoodsKz;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
